const common = require('../../utils/commonUtil.js');
const {requestList} = require('../../utils/netUtils.js');
Page({
   /* onShareAppMessage() {
        return {
            title: '挑选对胃口的艺术活动来这里就对了！',
            path: 'pages/exhibit-list/list'
        }
    },*/
    data: {
        list: [],
        type:'',
        filterBarShow:true,
        loading:true,
        total:0,
        page:0,
        filterData:{
            length:10,
            offset:0,
            relationId:100,
            status:100,
            selectStatus:100,
            keyword:''
        },
        imagePrefix: common.imagePrefix
    },
    showTitleByType: function (type) {
        let title ='展讯列表';
        let filterData = this.data.filterData;
        if(type==='new'){
            title = '新展来了';filterData.selectStatus = 101;
        }else if(type==='recommend'){
            title = '编辑推荐';filterData.selectStatus = 102;
        }else if(type==='want'){
            title = '大家想去';filterData.selectStatus = 103;
        }
        wx.setNavigationBarTitle({title:'活动内容'});
        this.setData({type,filterData},()=>{
            this.selectComponent('#filter-bar').setType(type);
        });
    },
    onLoad: function (option) {
        this.showTitleByType(option.type);

        requestList(this.data.filterData,res=>{
            console.log({type:'展讯列表返回:',res});
            this.gotDataAndFresh(res.data,false);
        });
        this.lastScorllTop = 0;
    },

    gotDataAndFresh:function(data,isClear){
        let tempList = data.dtoList;
        tempList.map(item=>{
            let timeStrObj = common.timeUtil.genStartEndTime(item.startTime,item.endTime);
            item.startTime =timeStrObj.start;
            item.endTime = timeStrObj.end;
        });
        let list;
        if(isClear){
            list = tempList||[];
        }else{
            list = this.data.list.concat(tempList);
        }
        this.setData({list,total:data.count,loading:false});
    },
    onReady: function () {
        setTimeout(()=>{this.setData({
            op: 1,
            mr:0
        })},400);
     },
    onShow: function () {

    },
    onHide: function () {
    },
    onUnload: function () {

    },
    onPullDownRefresh: function () {
        let resetedFilterData=this.data.filterData;
        resetedFilterData.offset=0;
        this.setData({ filterData:resetedFilterData,page:0,loading:true},()=>{
            requestList(this.data.filterData,res=>{
                console.log({type:'展讯列表下拉刷新后返回:',res});
                this.gotDataAndFresh(res.data,true);
                wx.stopPullDownRefresh();
            });
        });
    },
    onReachBottom: function () {
        this.next();
    },

    onFilterBarResult:function(r){//filter-bar筛选结果的回调
        console.log('on filter result:');
        console.log(r.detail);
        let filterData = this.data.filterData;
        if(filterData.status===r.detail.state.idx+100
            &&filterData.relationId ===r.detail.time.idx+100
            &&filterData.keyword === r.detail.filter.join(',')
            &&filterData.selectStatus === r.detail.sortt.idx+100){
            return;
        }
        filterData.status = r.detail.state.idx+100;
        filterData.relationId = r.detail.time.idx+100;
        filterData.selectStatus = r.detail.sortt.idx+100;
        filterData.keyword = r.detail.filter.join(',');
        filterData.offset = 0;
        this.setData({filterData,page:0},()=>{
            requestList(this.data.filterData,res=>{
                console.log({type:'展讯列表返回:',res});
                this.gotDataAndFresh(res.data,true);
                wx.pageScrollTo({
                    scrollTop: 0,
                    duration: 500
                });
            });
        });
    },

    next:function(){
        let page = this.data.page+1;
        let filterData=this.data.filterData;
        if(filterData.status===100){//展讯状态为全部时，接口不提供分页功能
            return;
        }
        filterData.offset=page*10;

        this.setData({filterData,page},()=>{

            console.log('next,filter:',this.data);
            requestList(this.data.filterData,res=>{
                console.log({type:'展讯列表返回:',res});
                this.gotDataAndFresh(res.data,false);
                this.onReady();
            });
        });
    },
    goDeail:function (e) {
        let id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: "/pages/exhibit-detail/detail?id="+id
        });
    },
   /* onPageScroll(e){
        common.debounce(this.onScroll(e),50000000,false);
    },
    onScroll(e){
        let top = e.scrollTop;
        if(top-this.lastScorllTop>0){
            console.log('up')
        }else if(top-this.lastScorllTop<0){
            console.log('down')
        }
        this.lastScorllTop = top;
    }*/
});
